﻿using CSCore;
using CSCore.DSP;
using CSCore.SoundIn;
using CSCore.SoundOut;
using CSCore.Streams;
using MSR.LST.Net.Rtp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace csharp_cscore_play
{
    class Program
    {
        [STAThread]
        static void rtp(string[] args)
        {
            string ParticipatorName = "a";
            
            IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 10000);
            IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.30.37"), 10000);
            RtpParticipant participant = new RtpParticipant(ParticipatorName + " Server Listener", "Listener " + ParticipatorName);
            RtpSession rtpSession = new RtpSession(remoteEP, participant, true, false);
            RtpSender rtp_sender = rtpSession.CreateRtpSender(ParticipatorName + " VOIP Reflector", PayloadType.dynamicAudio, null);
            
            using (ISoundIn capture = new WasapiCapture())
            {
                capture.Initialize();

                IWaveSource source = new SoundInSource(capture);
                WaveFormat sourceFormat = source.WaveFormat;
                WaveFormat targetFormat = new WaveFormat(
                    16000,
                    16,
                    sourceFormat.Channels,
                    AudioEncoding.Pcm,
                    sourceFormat.ExtraSize);

                using (IWaveSource encodedSource = new DmoResampler(source, targetFormat))
                {
                    capture.Start();

                    byte[] buffer = new byte[encodedSource.WaveFormat.BytesPerSecond / 4];

                    while (true)
                    {
                        int read = encodedSource.Read(buffer, 0, buffer.Length);
                        if (read > 0)
                        {
                            // todo
                            //Console.WriteLine(buffer[0]);
                            rtp_sender.Send(buffer);
                            Console.WriteLine(rtp_sender.PacketsSent);
                        }
                    }
                }
            }
        }
        
        [STAThread]
        static void Main(string[] args)
        {
            using (WasapiCapture capture = new WasapiCapture())
            {
                capture.Initialize();

                using (SoundInSource source = new SoundInSource(capture))
                {
                    using (WasapiOut soundOut = new WasapiOut())
                    {
                        capture.Start();

                        soundOut.Initialize(source);
                        soundOut.Play();

                        Console.ReadKey();
                    }
                }
            }
        }
        
        [STAThread]
        static void Main2(string[] args)
        {
            ISoundIn soundIn = null;
            soundIn = new WasapiCapture();
            soundIn.Initialize();
            soundIn.Start();

            //IWaveSource source = new SoundInSource(soundIn);

            /*WaveFormat sourceFormat = source.WaveFormat;
            WaveFormat targetFormat = new WaveFormat(
                16000,
                16,
                sourceFormat.Channels,
                AudioEncoding.Pcm,
                sourceFormat.ExtraSize);*/

            //using(IWaveSource encodedSource = new DmoResampler(source, targetFormat))
            using(IWaveSource encodedSource = new SoundInSource(soundIn))
            {
                byte[] buffer = new byte[encodedSource.WaveFormat.BytesPerSecond / 4];

                while (true)
                {
                    int read = encodedSource.Read(buffer, 0, buffer.Length);
                    if (read > 0)
                    {
                        // todo
                        Console.WriteLine(buffer[0]);
                    }
                }
                Thread.Sleep(250);
            }

            soundIn.Stop();
            soundIn.Dispose();
        }
    }
}
